package com.intellagric.service.iftmonitor.Impl.ift_ctrl_equipment;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftCtrlEquipmentLogMapper;
import com.intellagric.mapper.IftCtrlEquipmentMapper;
import com.intellagric.pojo.IftCtrlEquipment;
import com.intellagric.pojo.IftCtrlEquipmentLog;
import com.intellagric.pojo.IftCtrlEquipmentLogExample;
import com.intellagric.service.iftmonitor.ift_ctrl_equipment.CtrlEquipmentLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 控制设备操作记录管理服务实现
 */
@Service
public class CtrlEquipmentLogServiceImpl implements CtrlEquipmentLogService {

    @Autowired
    private IftCtrlEquipmentLogMapper ctrlEquipmentLogMapper;

    @Autowired
    private IftCtrlEquipmentMapper ctrlEquipmentMapper;

    /**
     * 添加控制设备操作记录
     * @param ctrlEquipmentLog
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addCtrlEquipmentLog(IftCtrlEquipmentLog ctrlEquipmentLog) {
        if (ctrlEquipmentLogMapper.insertSelective(ctrlEquipmentLog) == 1) {
            //监听日志标志位的值是否为1 1--消息已经被消费 0--没有被处理
            long sleepTime = 500;
            while(true) {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sleepTime += 500;
                IftCtrlEquipmentLog ctrlEquipmentLog1 = ctrlEquipmentLogMapper.selectByPrimaryKey(ctrlEquipmentLog.getId());
                if (ctrlEquipmentLog1.getDelFlag().equals("1") && ctrlEquipmentLog1.getReturnResult().equals("1")) {
                    //修改控制设备状态
                    IftCtrlEquipment ctrlEquipment = new IftCtrlEquipment();
                    ctrlEquipment.setId(ctrlEquipmentLog.getCtrlEquipmentId());
                    ctrlEquipment.setUpdateDate(new Date());
                    if ("0x81".equals(ctrlEquipmentLog.getOperateType()) || "0x02".equals(ctrlEquipmentLog.getOperateType())) {//开启
                        ctrlEquipment.setIsService("1");
                    } else {    //关闭
                        ctrlEquipment.setIsService("0");
                    }
                    ctrlEquipmentMapper.updateByPrimaryKeySelective(ctrlEquipment);
                    return ResponseMessage.success();
                }
                //大于10秒后执行不成功则表示失败
                if (sleepTime >= 10000 && ctrlEquipmentLog1.getDelFlag().equals("1")) {
                    return ResponseMessage.fail("设备已成功接收到指令，但未响应");
                }
            }
        } else {
            return ResponseMessage.fail("出现未知错误，请重试");
        }
    }

    /**
     * 删除控制设备操作记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteCtrlEquipmentLog(String id) {
        if (ctrlEquipmentLogMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改控制设备操作记录
     * @param ctrlEquipmentLog
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editCtrlEquipmentLog(IftCtrlEquipmentLog ctrlEquipmentLog) {
        if (ctrlEquipmentLogMapper.updateByPrimaryKeySelective(ctrlEquipmentLog) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询控制设备操作记录详细信息
     * @param id
     * @return IftCtrlEquipmentLog
     */
    @Override
    public IftCtrlEquipmentLog getCtrlEquipmentLogById(String id) {
        return ctrlEquipmentLogMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询控制设备操作记录详细信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentLogList() {
        IftCtrlEquipmentLogExample ctrlEquipmentLogExample = new IftCtrlEquipmentLogExample();
        ctrlEquipmentLogExample.createCriteria().andIdIsNotNull();
        List<IftCtrlEquipmentLog> ctrlEquipmentLogList = ctrlEquipmentLogMapper.selectByExample(ctrlEquipmentLogExample);
        return LayuiDataGridResult.success().add(ctrlEquipmentLogList,ctrlEquipmentLogList.size());
    }

    /**
     * 分页查询控制设备操作记录详细信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCtrlEquipmentLogPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        IftCtrlEquipmentLogExample ctrlEquipmentLogExample = new IftCtrlEquipmentLogExample();
        ctrlEquipmentLogExample.setOrderByClause("create_date DESC");
        ctrlEquipmentLogExample.createCriteria().andIdIsNotNull();
        List<IftCtrlEquipmentLog> ctrlEquipmentLogList = ctrlEquipmentLogMapper.selectByExample(ctrlEquipmentLogExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(ctrlEquipmentLogList);
        return result;
    }
}
